var ns, app;
app = new Vue({
    el: '#app',
    data: {
        ismoreshow: false,
        form: {
            id: 0,
            imagelist: [],
            processlist: [],
        },
        model:{},
        processlist:[],//工单轨迹
        operation:{},//权限
        imagelist:[],//图片

        timeOutList:[],
        task_imgList: [],
        total: false,
        isviewdetail: true,
        num: 6,
        rateValueist: ['极差', '差评', '一般 ', '优秀', '好评'],
        showorder: false,
        showproess:false,
        //收费记录
        moneylist:[],
        //领料记录 未关联生成出库单。则显示为：x 申请中   如果用户存在多次申请，则显示当前的领用数量。不显示第二次申请的状态
        materiallist: [],
        productlist:[],
        rateList: [],
        callBackData:[],
        typetj:0,//1-工单统计
        fhlist:[],//物料返还数据
        // operation: {
        //     canVisit: false,
        //     canClose: false,
        //     canSend: false,
        //     canAccept: false,
        //     canAfreshSend: false,
        //     canChuidan: false,
        //     canRefuse: false,
        //     canAcceptOrder: false,
        //     canComplete: false,
        //     canHandle: false
        // },
        servicetypename:'',//工单类型
        servicetypeid:'',//工单类型id
    },
    methods: {
        showOrderAll: function () {
            var that = this;
            that.showorder = !that.showorder;
        },
        showProessAll: function () {
            var that = this;
            that.showproess = !that.showproess;
        },
        get_data: function() {
            console.log('00000000000000');
            var that = this;
            var options = {};
            options.Id = that.form.id; 
            console.log(JSON.stringify(options));   
            ns.post(options, function (succeed, data, err) {
                console.log(JSON.stringify(data));
                console.log(JSON.stringify(err));
                if (succeed) {
                    that.model=data.model;
                    that.processlist=data.list;
                    // console.log();
                    that.operation=data.operation;
                    that.imagelist=data.fileList;

                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                // if (succeed) {
                //     that.form = data.form;
                //     that.timeOutList=data.timeOutList;
                //     that.task_imgList = data.form.imagelist;
                //     that.moneylist = data.form.feeLists;
                //     that.materiallist = data.form.productList;
                //     that.rateList = data.form.rateList;
                //     that.callBackData = data.form.callBackData;
                //     if (that.form.canHandle) {
                //         that.get_fhproject();查询是否可以物料返还
                //     }
                //     api.sendEvent({
                //         name: 'is_hidden_right_gongdan',
                //         extra: {
                //             keyId: 'right_gongdan',
                //             IsHidden: data.form.canTeamwork?data.form.canTeamwork:false
                //         }
                //     });
                //     setTimeout(function () {
                //         app.get_operation();
                //     }, 10)
                // } else if (err) {
                //     api.toast({
                //         msg: err,
                //         duration: 2000,
                //         location: 'bottom'
                //     });
                // }
            }, {
                // toast: true,
                command:'App/Service/GetDetail'
            });
        },
        get_operation: function () {
            var that = this;
            var options = {};
            options.action = "getserviceoperation";
            options.servicetype = that.form.FinalServiceTypeID;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.operation = data.operation;
                }else{
                }
            });
        },
        img_browser: function(item) {
            var that = this;
            var imgurls = [];
            imgurls.push(item.imgurl);
            imageBrowser.openImages({
                imageUrls: imgurls,
                showList: false,
                activeIndex: 0
            });
        },
        tel_phone: function(item) {
            api.call({
                type: 'tel_prompt',
                number: item.phonenumber
            });
        },
        tel_phone2: function(phonenumber) {
            api.call({
                type: 'tel_prompt',
                number: phonenumber
            });
        },
        open_mask_more: function() {
            var that = this;
            that.ismoreshow = true;
        },
        hide_mask_more: function() {
            var that = this;
            that.ismoreshow = false;
        },
        no_open: function() {
            api.alert({
                title: '提示',
                msg: '功能暂时未开放,敬请期待',
            }, function(ret, err) {});
        },
        openSchedule: function() {
            ns.openWin('home_schedule_frm', '日程');
        },
        openGongdan: function() {
            ns.openWin('home_task_con_frm', '任务');
        },
        openWin: function(item, params) {
            var that = this;
            if (item.url == '') {
                return;
            }
            params = params || {
                status: -1
            };
            ns.openWin(item.url, item.name, params);
        },
        do_grab: function() {
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "grabservice";
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.get_data();
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },

        //派单
        do_canpublish: function(type) {
            var that = this;
            ns.openWin('chooseserviceuser_frm', '接单人选择', {
                source: 'transfer',
                id: that.form.id,
                type: type,
                // ServiceType: that.form.FinalServiceTypeID
            });
        },
        //接单
        doAccpetOrder: function () {
            var that = this;
            var options = {};
            options.Id = that.form.id;
            console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                console.log(JSON.stringify(succeed));
                console.log(JSON.stringify(data));
                console.log(JSON.stringify(err));
                if (succeed) {
                        api.toast({
                            msg: "操作成功",
                            duration: 2000,
                            location: 'bottom'
                        });
                    that.get_data();
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中',
                command: 'App/Service/Accept'
            });

        },
     
        //退回
        doRefuseOrder: function () {
            var that = this;
            var options = {};
            options.ID = that.form.id;
            api.confirm({
                title: '提示',
                msg: '退回该工单，工单将流转至派单人。是否确认退回？',
                buttons: ['确认', '取消']
            }, function (ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        ns.openWin('renwugongdancomplete_frm', '拒绝', {
                            id: that.form.id,
                            issuggestion: that.form.issuggestion,
                            type: 4,//1 处理  2 完工  3 回访 4拒绝
                        });
                    }
                }
            });
           
        },
        //type:1 处理  2 完工  3 回访 4拒绝 5挂起 6恢复
        do_updateStatus:function(type){
            var that=this;
            var title='';
            if(type==1){
                title='录入处理记录'
            }
            if(type==2){
                title='录入完工记录'
            }
            if(type==3){
                title='录入回访记录'
            }
            if(type==5){
                title='录入挂起记录'
            }
            if(type==6){
                title='录入恢复记录'
            }
            if(type==7){
                title='录入关单记录'
            }
            ns.openWin('renwugongdancomplete_frm', title, {
                id: that.form.id,
                issuggestion: that.form.issuggestion,
                type: type,//1 处理  2 完工  3 回访 4拒绝 5挂起 6恢复
            });
        },
        //催单
        doUrgeOrder: function () {
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "jpushuser";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        api.toast({
                            msg: "操作成功",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    if (!data.status) {
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.get_data();
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        //协同
        open_xietong: function() {
            var that = this;
            ns.openWin('chooseserviceuser_frm', '协同处理', {
                source: 'transfer',
                id: that.form.id,
                type: 2,
                is_xietong: true,
                // ServiceType: that.form.FinalServiceTypeID
            });
        },
        //转交
        open_transfer: function() {
            var that = this;
            ns.openWin('chooseserviceuser_frm', '工单转交', {
                source: 'transfer',
                id: that.form.id,
                type: 3,
                // ServiceType: that.form.FinalServiceTypeID
            });
        },
        do_returnback: function() {
            var that = this;
            var options = {};
            options.ID = that.form.ID;
            options.action = "doreturnbackservice";
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.get_data();
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        do_open_fee: function() {
            var that = this;

            //查询当前工单状态
            var options = {};
            options.ID = that.form.ID;
            options.action = "getcustomerservicestatus";
            ns.post(options, function (succeed, data, err) {
                if (data.status) {
                    if (data.ServiceStatus == that.form.status) {
                        ns.openWin('customerservice_feelist_frm', '有偿收费', {
                            id: that.form.ID
                        });
                    }
                    else {
                        api.toast({
                            msg: "该工单状态已改变",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.get_data();
                } else {
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
          
        },
        do_open_product: function() {
            var that = this;
            ns.openWin('customerservice_productlist_frm', '领料详情', {
                id: that.form.ID
            });
        },
        do_add_fee: function () {
            var that = this;
            ns.openWin('renwugongdan_addfee_frm', '收费详情', {
                id: app.form.ID
            });
        },
        do_add_product: function () {
            var that = this;
            //查询当前工单状态
            var options = {};
            options.ID = that.form.ID;
            options.action = "getcustomerservicestatus";
            ns.post(options, function (succeed, data, err) {
                if (data.status) {
                    if (data.ServiceStatus == that.form.status) {
                        ns.openWin('chooseckproduct_frm', '物料领用申请', {
                            ID: that.form.ID,
                        });
                    }
                    else {
                        api.toast({
                            msg: "该工单状态已改变",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.get_data();
                } else {
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
          
        },
        do_return_product: function () {
            var that = this;
            //查询当前工单状态
            var options = {};
            options.ID = that.form.ID;
            options.action = "getcustomerservicestatus";
            ns.post(options, function (succeed, data, err) {
                if (data.status) {
                    if (data.ServiceStatus == that.form.status) {
                        ns.openWin('choosefhproduct_frm', '物料返还申请', {
                            ID: that.form.ID,
                        });
                    }
                    else {
                        api.toast({
                            msg: "该工单状态已改变",
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                    that.get_data();
                } else {
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        // 查询是否有物料返还数据，无则不显示返还按钮
        // get_fhproject: function () {
        //     var that = this;
        //     that.fhlist = [];
        //     var options = {
        //         action: 'getserviceproductlist',
        //         ServiceID: that.form.ID,
        //         KeyWords: '',
        //     };
        //     ns.post(options, function (succeed, data, err) {
        //         if (succeed) {
        //             that.fhlist = data.list;
        //         } 
        //     });
        // },

    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    imageBrowser = api.require('imageBrowser');
    app.form.id = api.pageParam.id || 0;
    // app.isviewdetail = api.pageParam.isviewdetail || false;
    app.typetj=api.pageParam.type;
    app.get_data();
    api.addEventListener({
        name: 'updata_task_con'
    }, function(ret, err) {
        if (ret) {
            app.get_data();
            app.ismoreshow = false;
        } else {
            api.toast({
                msg: err,
                duration: 2000,
                location: 'bottom'
            });

        }
    });
    api.addEventListener({
        name:'updata_task_con_close'
    }, function(ret){
        api.closeWin();
    })


    //协同
    // api.addEventListener({
    //     name: 'do_open_chooseserviceuser',
    // }, function (ret, err) {
    //     var options = {};
    //     options.ID = app.form.id;
    //     options.action = "getcustomerservicestatus";
    //     ns.post(options, function (succeed, data, err) {
    //         if (data.status) {
    //             if (data.ServiceStatus == app.form.status) {
    //                 ns.openWin('chooseserviceuser_frm', '协同人员选择', {
    //                     source: 'transfer',
    //                     id: app.form.ID,
    //                     type: 2,
    //                     is_xietong: true,
    //                     ServiceType: app.form.FinalServiceTypeID,
    //                 });
    //             }
    //             else {
    //                 api.toast({
    //                     msg: "该工单状态已改变",
    //                     duration: 2000,
    //                     location: 'bottom'
    //                 });
    //             }
    //             app.get_data();
    //         } else {
    //             api.toast({
    //                 msg: data.Msg,
    //                 duration: 2000,
    //                 location: 'bottom'
    //             });
    //         }
    //     });    
    // });


    api.addEventListener({
        name: 'dosaveservicetype'
    }, function (ret) {
        app.servicetypename = ret.value.item.text;
        app.servicetypeid = ret.value.item.id;
        app.do_editType();

    })

    api.sendEvent({
        name: 'is_task_read',
        extra: {
            key1: app.form.id,
        }
    });

};
